﻿using MQ.Core.Common;
using MQ.Core.Db;
using MQ.Core.Entity;
using MQ.Core.IRepository;
using MQ.Core.Util;
using System;
using System.Collections.Generic;
using System.Text;
using Dapper;

namespace MQ.Core.Repository
{
    public class DictionaryRepository:IDictionaryRepository
    {
        private readonly ISqlDb db;

        public DictionaryRepository(ISqlDb db)
        {
            this.db = db;
        }

        #region 查询数据
        public bool ExistByName(long parentId, string name)
        {
            string sql = $"select count(1) from sys_dictionary where parentid={parentId} and name='{name}'";
            return db.Exist(sql);
        }

        public bool ExistByValue(long parentId, string value)
        {
            string sql = $"select count(1) from sys_dictionary where parentid={parentId} and value='{value}'";
            return db.Exist(sql);
        }

        public IEnumerable<DictionaryEntity> GetList(string query)
        {
            string sql = "select * from sys_dictionary where 1=1";
            var json = query.ToJObject();
            if (!json["id"].IsEmpty())
            {
                sql += $" and id={json["id"].ToInt64()}";
            }
            if (!json["parentId"].IsEmpty())
            {
                sql += $" and parentId={json["parentId"].ToInt64()}";
            }
            if (!json["type"].IsEmpty())
            {
                sql += $" and typeId={json["type"].ToInt64()}";
            }
            if (!json["name"].IsEmpty())
            {
                sql += $" and name='{json["name"].ToString()}'";
            }
            if (!json["value"].IsEmpty())
            {
                sql += $" and value='{json["value"].ToString()}'";
            }
            return db.GetList<DictionaryEntity>(sql);
        }

        public IEnumerable<DictionaryEntity> GetPageList(Pagination pagination, string query)
        {
            string sql = "select * from sys_dictionary where 1=1";
            var json = query.ToJObject();
            if (!json["id"].IsEmpty())
            {
                sql += $" and id={json["id"].ToInt64()}";
            }
            if (!json["parentId"].IsEmpty())
            {
                sql += $" and parentId={json["parentId"].ToInt64()}";
            }
            if (!json["name"].IsEmpty())
            {
                sql += $" and name='{json["name"].ToString()}'";
            }
            if (!json["value"].IsEmpty())
            {
                sql += $" and value='{json["value"].ToString()}'";
            }
            if (!json["type"].IsEmpty())
            {
                sql += $" and typeId={json["type"].ToInt64()}";
            }
            return db.GetList<DictionaryEntity>(sql,pagination);
        }
        #endregion

        #region 提交数据
        public int Add(DictionaryEntity entity)
        {
            string sql = $"insert into sys_dictionary(parentid,name,value,status,[order],remark,typeid) values({entity.ParentId},'{entity.Name}','{entity.Value}',{entity.Status},{entity.Order},'{entity.Remark}',{entity.TypeId})";
            return db.Execute(sql);
        }

        public int Delete(long id)
        {
            string sql = $"delete from sys_dictionary where id={id}";
            return db.Execute(sql);
        }

        public int Delete(string ids)
        {
            string sql = $"delete from sys_dictionary where id in({ids})";
            return db.Execute(sql);
        }

        public int Delete(DictionaryEntity entity)
        {
            string sql = $"delete from sys_dictionary where id in={entity.Id}";
            return db.Execute(sql);
        }
        public int Update(DictionaryEntity entity)
        {
            string sql = $"update sys_dictionary set parentid={entity.ParentId},name='{entity.Name}',value='{entity.Value}',status={entity.Status},remark='{entity.Remark}',order={entity.Order},typeid={entity.TypeId} where id={entity.Id}";
            return db.Execute(sql);
        }
        #endregion
    }
}
